<template>
    <el-dialog :title="title" :visible="visible" width="50%" :show-close="true" :close-on-click-modal="false">
        <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="120px" class="demo-ruleForm">
            <el-form-item label="商品分类" prop="category_id">
                <el-select v-model="ruleForm.category_id" placeholder="请选择">
                    <el-option :value="i.value" :label="i.label" v-for="i in categoryList" />
                </el-select>
            </el-form-item>
            <el-form-item label="名称" prop='name'>
                <el-input v-model="ruleForm.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="商品图" prop="main_image">
                <Upload v-model="ruleForm.main_image" />
            </el-form-item>
            <el-form-item label="商品轮播图" prop="images">
                <Upload v-model="ruleForm.images" />
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
                <el-input type="number" v-model="ruleForm.price" placeholder="请输入地址">
                    <template slot="append">元</template>
                </el-input>
            </el-form-item>
            <el-form-item label="商品折扣价" prop="discount_price">
                <el-input type="number" v-model="ruleForm.discount_price" placeholder="请输入营业执照号码">
                    <template slot="append">元</template>
                </el-input>
            </el-form-item>
            <el-form-item label="配送范围" prop="delivery_range">
                <el-input type="number" v-model="ruleForm.delivery_range" placeholder="请输入配送范围">
                    <template slot="append">公里</template>
                </el-input>
            </el-form-item>
            <el-form-item label="营业状态" prop="status">
                <el-select v-model="ruleForm.status" placeholder="请选择">
                    <el-option label="营业中" :value="1" />
                    <el-option label="休息中" :value="0" />
                </el-select>
            </el-form-item>
            <el-form-item label="商户描述" prop="description">
                <el-input type="text" v-model="ruleForm.description" placeholder="请输入商户描述">
                </el-input>
            </el-form-item>
            <el-form-item label="联系人姓名" prop="contact_name">
                <el-input type="text" v-model="ruleForm.contact_name" placeholder="请输入联系人姓名">
                </el-input>
            </el-form-item>
            <el-form-item label="联系人电话" prop="contact_phone">
                <el-input type="tel" :maxlength="11" v-model="ruleForm.contact_phone" placeholder="请输入联系人电话">
                </el-input>
            </el-form-item>
            <el-form-item label="经度" prop="longitude">
                <el-input type="number" v-model="ruleForm.longitude" placeholder="请输入经度">
                </el-input>
            </el-form-item>
            <el-form-item label="纬度" prop="latitude">
                <el-input type="number" v-model="ruleForm.latitude" placeholder="请输入纬度">
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="submit" :loading="loading">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { merchantAdd, merchantEdit } from '../api/index';
import Upload from '@/components/Upload.vue';
export default {
    props: ['categoryList'],
    data() {
        return {
            isEdit: false,
            options: [],
            title: '',
            visible: false,
            loading: false,
            ruleForm: {
                category_id: null,
                name: null,
                main_image: null,
                images: null,
                price: null,
                business_license_number: null,
                status: null,
                description: null,
                contact_name: null,

                contact_phone: null,
                delivery_range: null,
                longitude: null,
                latitude: null,
            },
            rules: {
                category_id: [
                    { required: true, message: '请选择商品分类', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                ],
                main_image: [
                    { required: true, message: '请上传商品图', trigger: 'blur' }
                ],
                images: [
                    { required: true, message: '请上传商品轮播图', trigger: 'blur' }
                ],
                price: [
                    { required: true, message: '请输入商品价格', trigger: 'blur' }
                ],
                business_license: [
                    { required: true, message: '请上传营业执照', trigger: 'blur' }
                ],
                business_license_number: [
                    { required: true, message: '请输入营业执照号', trigger: 'blur' }
                ],
                status: [
                    { required: true, message: '请选择营业状态', trigger: 'blur' }
                ],
                description: [
                    { required: true, message: '请输入商户描述', trigger: 'blur' }
                ],
                contact_name: [
                    { required: true, message: '请输入联系人姓名', trigger: 'blur' }
                ],
                contact_phone: [
                    { required: true, message: '请输入联系人电话', trigger: 'blur' }
                ],
                delivery_range: [
                    { required: true, message: '请输入配送范围', trigger: 'blur' }
                ],
                longitude: [
                    { required: true, message: '请输入经度', trigger: 'blur' }
                ],
                latitude: [
                    { required: true, message: '请输入纬度', trigger: 'blur' }
                ],
            },
        }
    },
    components: { Upload },
    created() {

    },
    methods: {
        add() {
            this.visible = true;
            this.isEdit = false;
            this.title = '新增商户';
            let temp = {};
            Object.keys(this.ruleForm).forEach(key => {
                temp[key] = null
            })
            this.ruleForm = temp;
        },
        edit(data) {
            this.title = '修改商户';
            this.visible = true;
            this.isEdit = true;
            this.editId = data.id;
            Object.keys(this.ruleForm).forEach(key => {
                this.ruleForm[key] = data[key]
            })
        },
        close() {
            this.$refs.formRef.resetFields();
            this.visible = false;
        },
        submit() {
            this.$refs.formRef.validate(async (res) => {
                if (res) {
                    this.loading = true;
                    try {
                        const p = this.ruleForm
                        let res = this.isEdit ? await merchantEdit({ ...p, id: this.editId }) : await merchantAdd(p);
                        this.loading = false;
                        this.$refs.formRef.resetFields();
                        const msg = this.title + '成功';
                        this.$message({
                            type: 'success',
                            message: msg
                        })
                        this.close();
                        this.$emit('refresh', 1);
                    } catch (err) {
                        console.log(err);

                        this.loading = false;
                    }
                }
            })
        },
    },
}
</script>

<style lang='scss' scoped>
.avatar-uploader .el-upload {
    border: 1px dashed red;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>